package com.cdy.jxc_project.controller;

import com.cdy.jxc_project.domain.TMenu;
import com.cdy.jxc_project.json.R;
import com.cdy.jxc_project.service.TMenuService;
import com.cdy.jxc_project.utils.AddSysLog;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/*
 * @Project:jxc_project
 * @Author:cdy(缘生)
 * @Motto:放下杂念,只为迎接明天更好的自己
 * @Date:2023/4/21 12:20
 * */
@RestController
@RequestMapping("/tMenu")
public class TMenuController {

    @Resource
    private TMenuService tMenuService;

    /*
    侧边栏主菜单
     */
    @PostMapping("/queryTMenuByAdminId/{id}")
    public R queryTMenuByAdminId(@PathVariable Integer id){
        List<TMenu> tMenuList = tMenuService.queryTMenuByAdminId(id);
        return R.ok().message("菜单查询成功").data("menuList",tMenuList);
    }
    /*
    角色管理权限菜单
     */
    @PostMapping("/tMenuTypeQuery")
    public R tMenuTypeQuery(){
        List<TMenu> tMenuTypeQuery = tMenuService.tMenuTypeQuery();
        return R.ok().message("菜单查询成功").data("tMenuTypeQuery",tMenuTypeQuery);
    }

    /*
    以下是菜单管理
     */
    @PostMapping("/tMenuList")
    private R tMenuList(){
          List<TMenu> tMenuList = tMenuService.tMenuListQuery();
          return  R.ok().message("查询成功").data("tMenuList",tMenuList);
    }

    @PostMapping("/queryAll")
    private R queryAll(String name){
        List<TMenu> tMenuList = tMenuService.queryAll(name);
        return  R.ok().message("查询成功").data("tMenuList",tMenuList);
    }

    @DeleteMapping("/deleteTMenu/{id}")
    public R deleteTMenu(@PathVariable("id") Integer id){
        if (tMenuService.deleteTMenu(id)>0){
            return R.ok().message("删除成功");
        }else {
            return R.error().message("错误");
        }
    }

    @PostMapping("/insertAndUpdateTMenu")
    public R insertAndUpdateTCustomer(@RequestBody TMenu tMenu){
        try{
            if (tMenu.getId()==null){
                tMenuService.insertTMenu(tMenu);
                return R.ok().message("保存成功");
            }else {
                tMenuService.updateTMenu(tMenu);
                return R.ok().message("修改成功");
            }
        }catch (Exception e){
            e.printStackTrace();
            return R.error().message("程序出错");
        }
    }
}
